import { observable, action } from 'mobx';
import { SearchAdvanced, SearchPage} from 'weaver-mobile-page';
import * as  Apis from '../../apis/govern';
/*
 * @info: DomSearchStore
 * @Author: lusx
 * @Date: 2019-12-01 11:13:16
 * @Last modified by:   lusx
 * @Last modified time: 22-03-2019 01:46:24
 */
export default class  DomSearchStore {
    @observable searchAdvancedStore = new SearchAdvanced.Store({ route: '/pro/advanced',id:'idadvanced' });
    @observable searchPageStore = new SearchPage.Store({ route: '/pro/history' });
    @observable searchAdParams = {pageNum : 1};//表单域的键值对
    @observable searchKey = {};//SearchBar搜索框的当前值
    @observable searchResult = [];
    @observable initListView = function(){};//各列表组件初始化方法：请求长列表接口；
    listOtherParams = {};//请求长列表接口入参；
    searchInited = false;
    searchType = 6;//搜索类型
    listAnotherParams = {}//请求长列表接口入参；
    constructor(params) {
      //super(props)
      this.initDatas = this.initDatas.bind(this);
      this.searchInfo = this.searchInfo.bind(this);
      this.clearState = this.clearState.bind(this);
      //this.getTabKey = this.getTabKey.bind(this);19.1.21该方法无用
      this.searchHandle = this.searchHandle.bind(this);
      this.setSearchRoute = this.setSearchRoute.bind(this);
    }
    /**
     * 设置高级搜索、历史搜索页面路由地址
     * @method setSearchRoute
     * @for DomSearchStore
     * @param {参数类型} {params:''} 参数说明  当前页面的math.path;
     * @return {返回值类型}
     */
     setSearchRoute(params){
       this.searchAdvancedStore.route = `${params}/advanced`;
       this.searchPageStore.route = `${params}/advanced`;
     }
    /**
     * 初始化方法--初始化initListView；初始化高级搜索组件
     * @method initDatas
     * @for DomSearchStore
     * @param {参数类型} {params:FUNCTION} 参数说明  请求长列表接口的方法;
     * @return {返回值类型}
     */
    @action
    initDatas(params){
      this.initListView = params.initDatas;
      let path = params.pathname;
      this.listOtherParams = Object.assign({},params.getDataParams,{path});//19.1.22每次搜索时，重新new ListViewPage.Store,解决搜索时间，偶尔无数据问题
     
      this.searchInfo(path);
    }
    //渲染高级搜索组件
    @action
    searchInfo(path){
      this.searchAdvancedStore.responseKey = 'searchCondition';
      let param = {"isSuper":"1"};
      if(path.startsWith('/pro/')){
        param.pageFrom = 'project';
        param.categoryid = 0;
      }
      this.searchAdvancedStore.getData({
          url: '/api/govern/task/getTaskListConditions',
          method: 'GET',
          params:param,
        });
    }
    //搜索方法(第一页)
    /**
     * 获取搜索历史
     * @method getHistory
     * @for DomSearchStore
     * @param {参数类型} {} 参数说明  搜索类型;
     * @return {返回值类型}
     */
    @action
    submitAdvanced = (params)=>{
      this.searchKey = params ? params : {};
      this.listAnotherParams = {'isSuper':'1'};
      this.searchAdParams = {
        ...(this.searchAdvancedStore.form?this.searchAdvancedStore.form.formParams:{}),
        pageNum : 0
      };
      if(this.searchKey.bySearch === 1){//历史搜索
        //将历史搜索带带到搜索参数中
        this.listAnotherParams = {'isSuper':'0'};
        this.searchAdParams  = {...this.searchAdParams,'selectValue':this.searchKey.keyword }
      }
      this.searchResult = [];
      !this.searchInited && (this.searchInited = true);// 保证组件只初始化一次
      this.searchInfoNext().then(()=>{
        if(this.searchKey.bySearch === 1){//历史搜索
          this.addHistory(this.searchKey.keyword);
          this.getHistory();
        }else{
          this.setSearchHistory();
        }
      });
    }
    //搜索信息列表(下一页)
    @action
    searchInfoNext = ()=>{
      this.searchAdParams.pageNum++;
      return this.searchHandle();
    }
    //实际搜索列表方法
    searchHandle(){
      const props = {
        "doSearch":true,//前台区分列表渲染来源--搜素还是tab切换
        ...this.searchAdParams,
        ...this.listOtherParams,
        ...this.listAnotherParams,
        //"path":
      };
      this.clearState()//19.1.21点击搜索确定清空表单
      return this.initListView(props);
    }
    /**
     * 获取搜索历史
     * @method getHistory
     * @for DomSearchStore
     * @param {参数类型} {'searchtype':''} 参数说明  搜索类型;
     * @return {返回值类型}
     */
    @action
    getHistory = () => {
      const params = {'searchtype' : this.searchType};
      Apis.getHistorySearch(params).then((result) => {
          //result = {};
          this.historyList = [];
          result.historyData.map((data,i)=>{
            return this.historyList.push({
              key : i,
              name : data,
            });
          });
          this.searchPageStore.list.setState({data : this.historyList});
      });
    }
    /**
     * 添加搜索历史
     * @method addHistory
     * @for DomSearchStore
     * @param {参数类型} {"searchtext":String,'searchtype':''} 参数说明  搜索内容, 搜索类型;
     * @return {返回值类型}
     */
    addHistory = (keyword) =>{
      Apis.addHistorySearch({'searchtext':keyword,'searchtype':this.searchType})
    }
    //初始化搜索历史
    setSearchHistory = () => {
      const template = {
        default: [
          {
            key: 'col1',
            configs: [
              {
                key: 'col1_row1',
                configs: [
                  { key: 'name' },
                ],
              },
            ],
          },
        ],
      };
      this.searchPageStore.setState({ template });
      this.getHistory();
    }
    //历史搜索submit方法
    doSearchByKeyword = (keyword) => {
      this.submitAdvanced({'keyword' : keyword,'bySearch' : keyword ? 1 : 0});
    }
    //历史搜索onchange方法
    updateSearchGovSubject = (value) => {
      this.searchAdvancedStore.form.updateField('docsubject', value);
    }
    /**
     * 清除搜索历史
     * @method getHistory
     * @for DomSearchStore
     * @param {参数类型} {'searchtype':''} 参数说明  搜索类型;
     * @return {返回值类型}
     */
    clearSearchHistory = () => {
      const params = {'searchtype' : this.searchType};
      Apis.clearHistory(params).then((result) => {
            this.historyList = [];
            this.searchPageStore.list.setState({data : this.historyList});
        });
    }
    //清理方法
    @action
    clearState(callback){
      this.searchAdParams = {};
      this.searchAdvancedStore.form?this.searchAdvancedStore.form.resetFields():{};//重置高级搜索表单
      this.listAnotherParams = {};
      if(callback){
        callback();
      }
    }
}
